package me.pplay.playtalk.activity;

import java.util.ArrayList;
import java.util.List;

import me.pplay.playtalk.R;
import me.pplay.playtalk.adapter.NotificationAdapter;
import me.pplay.playtalk.bean.Chat;
import me.pplay.playtalk.bean.NotificationEntity;
import me.pplay.playtalk.data.Globals;
import me.pplay.playtalk.database.FriendDb;
import me.pplay.playtalk.database.NotificationDb;
import me.pplay.playtalk.service.PlayTalkService;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;

public class NotificationActivity extends BaseActivity {
	private ImageButton btn_back;
	private ListView listView;
	private List<NotificationEntity> notiList;
	
	private final int SETADAPTER = 1;
	
	private FriendDb dbFriend;
	private NotificationDb dbNoti;
	private NotificationAdapter adapter;
	
	
	Handler handler = new Handler(){

		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			switch (msg.what) {
			case SETADAPTER:
				if(adapter == null){
					adapter = new NotificationAdapter(NotificationActivity.this, notiList);
					listView.setAdapter(adapter);
				}else{
					adapter.notifyDataSetChanged();
				}
					
				break;

			case Globals.SETADAPTER:
				getDatas();
				break;
			}
		}
		
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.layout_notification);
		findViews();
		initDatas();
		clickEvents();
		System.out.println("ddddddd::"+Globals.skyid);
	}
	
	private void findViews(){
		btn_back	= (ImageButton)findViewById(R.id.btn_back);
		listView 	= (ListView)findViewById(R.id.listview);
	}
	
	private void initDatas(){
		dbFriend	= new FriendDb(this);
		dbNoti		= new NotificationDb(this);
		notiList	= new ArrayList<NotificationEntity>();
		getDatas();
	}
	
	
	private void getDatas(){
		notiList	= dbNoti.queryList(0);  //纯文本
		List<Chat> friendList = dbFriend.queryList(2);
		int count 	= friendList.size();
		for (int i = count-1; i >= 0; i--) {
			Chat friend = friendList.get(i);
			notiList.add(new NotificationEntity(2, friend.getSkyid(), friend.getIcon(), friend.getName(), friend.getMessage(), friend.getTime()));
		}
		handler.sendEmptyMessage(SETADAPTER);
	}
	
	private void clickEvents(){
		btn_back.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				NotificationActivity.this.finish();
			}
		});
		
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
				notiList.remove(arg2);
				handler.sendEmptyMessage(SETADAPTER);
			}
		});
	}

	
	@Override
	protected void onStart() {
		PlayTalkService.getInstance().setIsNotification(true);
		PlayTalkService.getInstance().setHandler(handler);
		super.onStart();
	}
	
	@Override
	protected void onStop() {
		PlayTalkService.getInstance().setIsNotification(false);
		super.onStop();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}
}
